package com.wzw.conference.utils;

import com.wzw.conference.exception.ConferenceException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author wzw
 * @date 2023/3/29
 */
public class FileReaderUtils {

  private FileReaderUtils() {

  }

  public static List<String> readLineFromFile(String fileName) {
    if (StringUtils.isBlank(fileName)) {
      throw new ConferenceException("文件名不能为空");
    }
    try {
      String filePath = Objects.requireNonNull(
          FileReaderUtils.class.getClassLoader().getResource(fileName)).getPath();
      return FileUtils.readLines(new File(filePath), StandardCharsets.UTF_8);
    } catch (NullPointerException e) {
      throw new ConferenceException("文件不存在");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return Collections.emptyList();
  }
}
